package action.back;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import models.Orgn;
import models.Zone;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import utils.StringHelper;
import config.Constant;

public class OrgnAction extends DefaultAction {
	
	private Orgn orgn;
	
	public String index() {
		return "index";
	}
	
	@SuppressWarnings("unchecked")
	public void getTrees() {
		boolean if_orgn   = StringHelper.parseBoolean(request.getParameter("if_orgn"));
		Integer zone_id   =  Integer.parseInt(request.getParameter("zone_id"));
		Integer parent_id = Integer.parseInt(request.getParameter("parent_id"));

		List<Orgn> orgns = new ArrayList<Orgn>();
		List<Zone> zones = new ArrayList<Zone>();
		if(!if_orgn){
			if (zone_id == null || zone_id.intValue() == 0) {
				zones = getCommonService().findAll(Zone.class, "position asc", "parent is null",null,"parent");
			} else {
				zones = getCommonService().findAll(Zone.class, "position asc", "parent.id = ?",new Object[]{zone_id},"parent");
			}
		}
		
		if (parent_id == null || parent_id.intValue() == 0) {
			orgns = getCommonService().findAll(Orgn.class, "position asc", "zone.id = ? and parent is null",new Object[]{zone_id});
		} else {
			orgns = getCommonService().findAll(Orgn.class, "position asc", "zone.id = ? and parent.id=?",new Object[]{zone_id,parent_id});
		}
		
		List<Map> trees = new ArrayList<Map>();
		
		for (Zone zone : zones) {
			Map<String, Object> tree = new HashMap<String, Object>();
			tree.put("id", "Z_" + zone.getId());
			tree.put("text", zone.getName());
			tree.put("iconCls", "admin-tool-zone");
			tree.put("leaf", zone.getSubZoneCount() + zone.getSubOrgnCount() == 0);
			tree.put("if_orgn", Constant.FALSE);
			tree.put("zone_id", zone.getId());
			tree.put("parent_id", "0");
			trees.add(tree);
		}
		
		for (Orgn orgn : orgns) {
			Map<String, Object> tree = new HashMap<String, Object>();
			tree.put("id", orgn.getId());
			tree.put("text", orgn.getName());
			tree.put("iconCls", "admin-tool-orgn");
			tree.put("leaf", orgn.getSubOrgnCount() == 0);
			tree.put("if_orgn", Constant.TRUE);
			tree.put("zone_id", orgn.getZone().getId());
			tree.put("parent_id", orgn.getId());
			trees.add(tree);
		}
		
		JSONArray jsonObject = JSONArray.fromObject(trees);
		String jsonString = jsonObject.toString();
		renderJson(jsonString);
		
	}
	
	@SuppressWarnings("unchecked")
	public void getChildren() {
		Integer parent_id = Integer.parseInt(request.getParameter("parent_id"));
		Integer zone_id = Integer.parseInt(request.getParameter("zone_id"));    
	    List<Orgn> orgns;
	    if (parent_id == null || parent_id.intValue() == 0) {
			orgns = getCommonService().findAll(Orgn.class, "position asc", "parent is null and zone.id=?",new Object[]{zone_id},"parent","zone");
		} else {
			orgns = getCommonService().findAll(Orgn.class, "position asc", "parent.id = ? and zone.id=?",new Object[]{parent_id,zone_id},"parent","zone");
		}
	    List<Map<String, Object>> jsonList = new ArrayList<Map<String,Object>>();
	    for(Orgn m : orgns){
	    	Map<String, Object> map = new HashMap<String, Object>();
	    	map.put("orgn", m);
	    	
	    	jsonList.add(map);
	    }
	    JsonConfig config = new JsonConfig();
	    config.setExcludes(new String[]{"parent","zone"});
	    JSONArray jsonObject = JSONArray.fromObject(jsonList,config);
		String jsonString = jsonObject.toString();
		renderJson(jsonString);
	}
	


	public void update() {
		Orgn orgnPo = getCommonService().getById(Orgn.class, orgn.getId(), "parent","zone");
		orgn.setZone(orgnPo.getZone());
		orgn.setParent(orgnPo.getParent());
		getCommonService().update(orgn);
		renderJson("{success:true,id:"+orgn.getId()+",name:'"+orgn.getName()+"'}");
	}
	
	public void create() {
		
		Integer zone_id   =  Integer.parseInt(request.getParameter("zone_id"));
		Integer parent_id = Integer.parseInt(request.getParameter("parent_id"));
		
		if(parent_id > 0){
			Orgn parent = new Orgn();
			parent.setId(parent_id);
			orgn.setParent(parent);
		}
		Zone zone = new Zone();
		zone.setId(zone_id);
		orgn.setZone(zone);
		getCommonService().save(orgn);

		renderJson("{success:true,id:"+orgn.getId()+",zone_id:"+zone_id+",parent_id:"+orgn.getId()+"}");
	}
	
	

	public void delete() {
		for(Integer id : ids){
			getCommonService().executeUpdate("delete Orgn where parent.id = ?", id);
			getCommonService().executeUpdate("delete Orgn where id = ?", id);
		}
		
		renderJson("{success:true}");
	}
	

	public Orgn getOrgn() {
		return orgn;
	}

	public void setOrgn(Orgn orgn) {
		this.orgn = orgn;
	}
}
